package com.lgq.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.net.URLDecoder;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



/**
 * @author 刘光强
 * @date 2012-3-21 下午4:44:21
 * @version 1.0
 * @处理头像上传的servlet
 **/
public class Avatar extends HttpServlet {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    // 头像存放目录
    // 缓存目录在下面的tmp目录中
    private String dirString = "D:/avatar";
    private String tmpdirString = dirString + "/tmp";
    /**
     * 处理头像上传的servlet
     */
    public void service(HttpServletRequest request, HttpServletResponse response) {
	PrintWriter out = null;
	logger.debug("上传头像");
	try {
	    out = response.getWriter();
	} catch (IOException e1) {
	    logger.error("上传头像获取输出流失败" + e1.getMessage());
	    return;
	}
	String username = request.getParameter("input");
	if (StringUtils.isEmpty(username)) {
	    logger.error("上传头像时获取用户名失败");
	    return;
	} else {
	    username = URLDecoder.decode(username);
	}
	String action = request.getParameter("a");
//	FileItem item = null;
	if (StringUtils.isEmpty(action)) {
	    return;
	} else if ("uploadavatar".equals(action)) {// 上传临时图片
//	    logger.debug("上传临时头像,用户名：" + username);
//	    if (!FileUploadUtil.createDirIfNoExist(tmpdirString)) {
//		logger.error("创建临时目录失败");
//		return;
//	    }
//	    ;
//	    File tempAvatar = new File(tmpdirString + "/" + username + ".jpg");
//	    FileUploadUtil fileUploadUtil = new FileUploadUtil(new File(
//		    tmpdirString), 2048);
//	    try {
//		fileUploadUtil.parse(request, "utf-8");
//		item = fileUploadUtil.getFileItem("Filedata");
//		if (!item.isFormField()) {
//		    item.write(tempAvatar);
//		}
//		logger.debug("临时文件的路径为" + StringUtil.getSite(request)
//			+ "D:/avatar" + "/tmp/"
//			+ tempAvatar.getName());
//		out.write(StringUtil.getSite(request)
//			+ "D:/avatar" + "/tmp/"
//			+ tempAvatar.getName());
//	    } catch (Exception e) {
//		logger.info("上传临时头像失败,用户名：" + username + "信息" + e.getMessage());
//		return;
//	    }
		out.write("http://127.0.0.1:8080/myspace/images/3big.jpg");
	} else if ("rectavatar".equals(action)) {// 缩略图
//	    // 删除临时图片
//	    File tempAvatar = new File(tmpdirString + "/" + username + ".gif");
//	    tempAvatar.deleteOnExit();
//	    logger.info("上传缩略头像,用户名：" + username);
//	    if (!FileUploadUtil.createDirIfNoExist(dirString)) {
//		logger.error("创建头像目录失败");
//		out.print("<?xml version=\"1.0\" ?><root><face success=\"0\"/></root>");
//		out.close();
//		return;
//	    }
//	    if (!FileUploadUtil.createDirIfNoExist(dirString + "/" + username)) {
//		logger.error("创建头像目录失败");
//		out.print("<?xml version=\"1.0\" ?><root><face success=\"0\"/></root>");
//		out.close();
//		return;
//	    }
//	    ;
//	    File smallAvatar = new File(dirString + "/" + username + "/"
//		    + username + "_small.gif");
//	    File middleAvatar = new File(dirString + "/" + username + "/"
//		    + username + "_middle.gif");
//	    File bigAvatar = new File(dirString + "/" + username + "/"
//		    + username + "_big.gif");
//	    String avatar1 = request.getParameter("avatar1");// 大
//	    String avatar2 = request.getParameter("avatar2");// 中
//	    String avatar3 = request.getParameter("avatar3");// 小
//	    boolean smallOK = writeAvatar(smallAvatar, avatar3);
//	    boolean middleOK = writeAvatar(middleAvatar, avatar2);
//	    boolean bigOK = writeAvatar(bigAvatar, avatar1);
//	    if (smallOK && middleOK && bigOK) {
//		CsipUser user = iUserDao.getUserByName(username);
//		if (user == null) {
//		    logger.error("上传头像时获取用户失败");
//		    out.print("<?xml version=\"1.0\" ?><root><face success=\"0\"/></root>");
//		    out.close();
//		}
//		user.setCyptUserHead(StringUtil.getSite(request)
//			+ ConfigUtil.get("virtualavatarpath") + "/" + username
//			+ "/" + bigAvatar.getName());
//		boolean updateOK = iUserDao.updateUser(user);
//		if (updateOK) {
//		    out.print("<?xml version=\"1.0\" ?><root><face success=\"1\"/></root>");
//		} else {
//		    out.print("<?xml version=\"1.0\" ?><root><face success=\"0\"/></root>");
//		}
//		out.close();
//	    } else {
//		out.print("<?xml version=\"1.0\" ?><root><face success=\"0\"/></root>");
//		out.close();
//	    }
//	}
//    }
//
//    private boolean writeAvatar(File avatarFile, String avatar) {
//	if (StringUtils.isEmpty(avatar)) {
//	    return false;
//	}
//	FileOutputStream fos = null;
//	try {
//	    fos = new FileOutputStream(avatarFile);
//	    byte[] bs = flashDataDecode(avatar);
//	    fos.write(bs, 1, bs.length - 1);
//	} catch (Exception e) {
//	    return false;
//	} finally {
//	    try {
//		if (fos != null) {
//		    fos.flush();
//		    fos.close();
//		    fos = null;
//		}
//	    } catch (Exception e) {
//	    }
	}
//	return true;
    }

    private byte[] flashDataDecode(String dataStr) {
	if ((dataStr.length() & 0x01) == 0x01) {
	    dataStr = new String(dataStr + "0");
	}
	BigInteger cI = new BigInteger(dataStr, 16);
	byte[] data = cI.toByteArray();
	return data;
    }

}
